<template>
  <div class="material_track">
    <one-title title="物料入库管理" @refreshData="refresh">
      <div class="mes_btn">
        <el-button
          v-btn-permission="'requestAgain'"
          @click="requestAgain">
          <i class="iconfont icon-btn_issued"></i>
          重新发起请求
        </el-button>
        <disabled-btn
          v-btn-permission="'cancelRequest'"
          :judge="cancelBtnStatus"
          iconClass="iconfont icon-btn_return"
          btnText="撤销申请"
          @judgeClick="cancelRequest"></disabled-btn>
        <el-button
          v-btn-permission="'launchApply'"
          type="success" @click="launchApply" :disabled="cancelStatus">
          <i class="iconfont icon-btn_addnew"></i>
          入库申请
        </el-button>
      </div>
    </one-title>
    <div class="fixed_top_56">
      <mes-search-comp
        :searchList="searchList"
        ref="mesSearch"
        :searchResult="searchResult"
        @searchInfo="_info">
      </mes-search-comp>
      <div class="table_container">
        <el-table
          :data="listData.list"
          border
          stripe
          ref="mesTable"
          v-loading="isLoading"
          element-loading-text="数据加载中"
          style="width: 100%"
          :max-height="tableMaxHeight"
          @sort-change="_sort"
          :row-class-name="({row}) => !selectData.some((item) => item.id === row.id) || 'line_selected'"
          @row-click="(row) => $refs.mesTable.toggleRowSelection(row)"
          @selection-change="handleSelectionChange"
          @filter-change="handleFilterChange">
          <el-table-column
            type="selection"
            width="40"
            align="center">
          </el-table-column>
          <mes-col-comp
            :key="index"
            v-for="(column, index) in tableColumns"
            :column="column"
          ></mes-col-comp>
          <el-table-column
            :resizable="false"
            fixed="right"
            label="操作"
            prop="phone"
            min-width="80"
            align="center"
            show-overflow-tooltip>
            <template slot-scope="scope">
            <span class="jump_item" @click.stop="openDetail(scope.row)">
              入库明细
            </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <mes-pagination
        @updateData="init"
        :currentPage="listData.pageNo"
        :total="listData.count || 0"
        :pageSize="listData.pageSize">
      </mes-pagination>
      <!--物料入库-->
      <storage-apply ref="storageApply" @submitSuccess="init"></storage-apply>
      <!--入库详情-->
      <apply-detail ref="applyDetail"></apply-detail>
    </div>
  </div>
</template>

<script>
/* 字典组件 */
import {MaterialState} from '@/components/mesDict'
import tableColumns from './tableColumns'
import StorageApply from './components/StorageApply'
import ApplyDetail from './components/ApplyDetail'

export default {
  name: 'MaterialStorage',
  components: {
    MaterialState,
    StorageApply,
    ApplyDetail
  },
  data() {
    return {
      // 列表数据加载中
      isLoading: false,
      // 搜索参数
      searchData: {},
      // 搜索结果数
      searchResult: NaN,
      // 排序参数
      sortData: {},
      // 选中的数据
      selectData: [],
      // 表格列
      tableColumns: tableColumns,
      cancelStatus: false, // 撤销状态
    }
  },
  computed: {
    // 列表数据
    listData() {
      return this.$store.state.materialStorage.listData
    },
    // 清单明细
    materialDetail() {
      return this.$store.state.materialTrack.materialDetail
    },
    // 查询条件
    searchList() {
      let searchList = this.tableColumns || []
      return searchList
    },
    // 验证是否已选
    _selected() {
      if (!this.selectData.length) return {msg: '请选择一条申请！'}
    },
    // 获取序列号按钮状态
    cancelBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      let msg = '只能撤销"待入库"的申请'
      if (this.selectData.some((item) => item.storeStatusValue !== 'ToBeStored')) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
  },
  mounted() {
    this.init()
  },
  methods: {
    // 刷新方法
    refresh() {
      this.$refs.mesSearch.clean()
      this.$refs.mesTable.clearSort()
      this.searchData = {}
      this.sortData = {}
      this.init()
    },
    // 列筛选回调
    handleFilterChange(filters) {
      this.sortData = {}
      this.$refs.mesTable.clearSort()
      let filterParams = this.$filterParams(this.searchList, filters)
      Object.assign(this.searchData, filterParams.search)
      Object.assign(this.sortData, filterParams.order)
      this.init()
    },
    // 初始化
    init(page) {
      this.isLoading = true
      let pageObj = page || {
        pageNo: 1,
        pageSize: this.listData.pageSize || 15,
      }
      let postData = {
        method: 'post',
        url: 'a/materialstore/materialStore/list',
        data: {
          pageNo: pageObj.pageNo,
          pageSize: pageObj.pageSize
        },
        module: 'materialStorage',
        state: 'listData'
      }
      if (Object.keys(this.searchData).length > 0) {
        postData.data.query = this.searchData
      }
      if (Object.keys(this.sortData).length > 0) {
        postData.data.sort = this.sortData
      }
      this.$store.dispatch('saveData', postData).then((res) => {
        if (Object.keys(this.searchData).length > 0) {
          this.searchResult = res.count
        } else {
          this.searchResult = NaN
        }
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 搜索
    _info(data) {
      this.searchData = data
      this.init()
    },
    // 复选框选中
    handleSelectionChange(val) {
      this.selectData = val
    },
    // 排序
    _sort(obj) {
      this.sortData = this.$getSortOrder(obj)
      this.init()
    },
    // 撤销申请
    cancelRequest() {
      if (!this.selectData.length) return {msg: '请选择一条申请！'}
      let ids = this.selectData.map((item) => item.id)
      this.confirmOpen('选中的记录将被撤销, 是否继续?', '撤销提示').then(() => {
        this.cancelStatus = true
        this.$store.dispatch('saveData', {
          method: 'post',
          url: 'a/materialstoreinventory/materialStoreInventory/cancelStoreApply',
          data: {materialStoreIdList: ids},
          noCommit: true
        }).then(() => {
          this.tipMessage('success', '撤销成功')
          this.init()
        }).finally(() => {
          this.cancelStatus = false
        })
      })
    },
    // 查看详情
    openDetail(val) {
      this.$refs.applyDetail.openDialog(val)
    },
    // 发起申请
    launchApply() {
      this.$refs.storageApply.openDialog()
    },
    // 重新发起请求
    requestAgain() {
      this.isLoading = true
      this.$store.dispatch('saveData', {
        method: 'post',
        url: 'a/materialstore/materialStore/allrequestagain',
        noCommit: true
      }).then(() => {
        this.tipMessage('success', '发起成功')
        this.init()
      }).finally(() => {
        this.isLoading = false
      })
    },
    /**
     * 提示
     * @param type 提示类型
     * @param str 提示内容
     */
    tipMessage(type, str) {
      this.$message({
        type: type,
        message: str,
        duration: 1500,
        center: true
      })
    }
  }
}
</script>

<style lang="less">

</style>
